package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Override
    public BusinessDataVO businessData(LocalDate begin , LocalDate end) {
        BusinessDataVO businessDataVO = new BusinessDataVO();
        //查询新增用户
        Integer newUsers = userMapper.getNewUsers(begin.atStartOfDay(), end.atStartOfDay().plusHours(23).plusMinutes(59).plusSeconds(59));
        businessDataVO.setNewUsers(newUsers);

        //查询今天的订单总数
        List<Orders> orders = orderMapper.finALlInTime(begin.atStartOfDay(), end.atStartOfDay().plusHours(23).plusMinutes(59).plusSeconds(59));
        //查询今天的有效订单总数
        List<Orders> orders1 = orderMapper.turnoverStatistics(begin.atStartOfDay(), end.atStartOfDay().plusHours(23).plusMinutes(59).plusSeconds(59));
        businessDataVO.setValidOrderCount(orders1.size());
        businessDataVO.setOrderCompletionRate((orders1.size()*1.0)/orders.size());
        //统计有效订单的amount
        Double amount = 0.0;
        for (Orders order : orders1) {
            amount += order.getAmount().doubleValue();
        }
        businessDataVO.setTurnover(amount);
        businessDataVO.setUnitPrice(amount/orders1.size());
        return businessDataVO;
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        //查询今天的所有订单
        List<Orders> orders = orderMapper.getByTime(LocalDateTime.now().withHour(0).withMinute(0).withSecond(0), LocalDateTime.now().withHour(23).withMinute(59).withSecond(59));
        orderOverViewVO.setAllOrders(orders.size());

        Map<Integer, Integer> map = new HashMap<>();
        orders.forEach(orders1 -> {
            if(!map.containsKey(orders1.getStatus())){
                map.put(orders1.getStatus(), 1);
            }else {
                map.put(orders1.getStatus(), map.get(orders1.getStatus())+1);
            }
        });
        orderOverViewVO.setWaitingOrders(map.get(Orders.TO_BE_CONFIRMED));
        orderOverViewVO.setDeliveredOrders(map.get(Orders.CONFIRMED));
        orderOverViewVO.setCompletedOrders(map.get(Orders.COMPLETED));
        orderOverViewVO.setCancelledOrders(map.get(Orders.CANCELLED));
        return orderOverViewVO;
    }

    @Override
    public DishOverViewVO overviewDishes() {
        return DishOverViewVO.builder().sold(dishMapper.getStartSaleDishCount()).discontinued(dishMapper.getStopSaleDishCount()).build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        return SetmealOverViewVO.builder().sold(setmealMapper.getStartSaleSetmealCount()).discontinued(setmealMapper.getStopSaleSetmealCount()).build();
    }
}
